package com.movitech.mobile.cz.base.supplier.bizservice.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.DateUtils;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.modules.config.EnumConstant;
import com.movitech.mobile.cz.base.supplier.bizservice.SSuppliersSubmitOldSysInterfaceService;
import com.movitech.mobile.cz.base.supplier.entity.SSupplierContactInfo;
import com.movitech.mobile.cz.base.supplier.entity.SSupplierManual;
import com.movitech.mobile.cz.base.supplier.entity.SSupplierObtainInformation;
import com.movitech.mobile.cz.base.supplier.entity.SSuppliers;
import com.movitech.mobile.cz.base.supplier.service.SSupplierContactInfoService;
import com.movitech.mobile.cz.base.supplier.service.SSupplierObtainInformationService;
import com.movitech.mobile.cz.base.syndata.SynDataService;
import com.movitech.mobile.cz.base.syndata.SynSupplierDataVo;
import com.movitech.mobile.cz.base.syndata.SynSupplierToMdmDataVo;
import com.movitech.mobile.cz.base.syndata.mdmmultivo.Multicode;
import com.movitech.mobile.cz.base.sys.entity.SysDict;
import com.movitech.mobile.cz.base.sys.service.SysAreaService;
import com.movitech.mobile.cz.base.sys.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SSuppliersSubmitOldSysInterfaceServiceImpl implements SSuppliersSubmitOldSysInterfaceService {
    @Autowired
    private SynDataService synDataService;
    @Autowired
    private SSupplierObtainInformationService sSupplierObtainInformationService;

    @Autowired
    @Qualifier("sysDictService")
    private SysDictService sysDictService;

    @Autowired
    private SysAreaService sysAreaService;
    @Autowired
    private SSupplierContactInfoService sSupplierContactInfoService;

    /**
     * 对接五个三的供应商插口
     *
     * @param sSuppliers
     * @param isNew
     * @return
     */
//    @Override
    /*public boolean abutmentSuplierData(SSuppliers sSuppliers, boolean isNew) {
        SynSupplierDataVo synSupplierDataVo = new SynSupplierDataVo();
        synSupplierDataVo.setSc_Code(sSuppliers.getCompanyCode());
        synSupplierDataVo.setId(sSuppliers.getId());
        synSupplierDataVo.setSc_Area(sysAreaService.getSysAreaCodesByIds(sSuppliers.getUndertakeAreas()));
        synSupplierDataVo.setSc_Level(sSuppliers.getStarLevel());
        synSupplierDataVo.setSc_Name(sSuppliers.getCompanyName());
        if (StringUtils.isNotEmpty(sSuppliers.getBusinessLicenseNumber())) {
            synSupplierDataVo.setSc_LicenseNo(sSuppliers.getBusinessLicenseNumber());
        } else {
            synSupplierDataVo.setSc_LicenseNo(sSuppliers.getSocialCreditCode());
        }

        if (sSuppliers.getEnterpriseRegistrationYear() != null) {
            synSupplierDataVo.setSc_StartupDate(DateUtils.formatDate(sSuppliers.getEnterpriseRegistrationYear()));
        }

        if (sSuppliers.getCreateDate() != null) {
            synSupplierDataVo.setSc_CheckinDate(DateUtils.formatDate(sSuppliers.getCreateDate()));
        }

        if (StringUtils.isNotEmpty(sSuppliers.getSupplierSourceInfo())) {
            synSupplierDataVo.setSc_RecommendPerson(sSuppliers.getSupplierSourceInfo());
        } else {
            SSupplierObtainInformation sSupplierObtainInformation = sSupplierObtainInformationService.selectBySupplierId(sSuppliers.getId());
            if (sSupplierObtainInformation != null) {
                if (StringUtils.isNotEmpty(StringUtils.object2StringNotNull(sSupplierObtainInformation.getRecommenderType()))) {
                    SysDict sysDict = sysDictService.getSysDictByValueAndType(StringUtils.object2StringNotNull(sSupplierObtainInformation.getRecommenderType()), CommonConstants.SYS_DICT_TYPE_RECOMMEND_TYPE);
                    if (sysDict != null) {
                        synSupplierDataVo.setSc_RecommendPerson(sysDict.getDictLabel()); //推荐类型
                    }
                }
            }
        }

        //企业类别
        if (EnumConstant.enterpriseCharacterize.enterpriseCharacterize1.equals(sSuppliers.getEnterpriseCharacterize())) {
            synSupplierDataVo.setSc_EquityType(1); //内资
        } else if (EnumConstant.enterpriseCharacterize.enterpriseCharacterize2.equals(sSuppliers.getEnterpriseCharacterize())) {
            synSupplierDataVo.setSc_EquityType(3); //外资
        } else if (EnumConstant.enterpriseCharacterize.enterpriseCharacterize3.equals(sSuppliers.getEnterpriseCharacterize())) {
            synSupplierDataVo.setSc_EquityType(2); //合资
        } else if (EnumConstant.enterpriseCharacterize.enterpriseCharacterize4.equals(sSuppliers.getEnterpriseCharacterize())) {
            synSupplierDataVo.setSc_EquityType(4); //民营企业
        }

        synSupplierDataVo.setSc_taxpayerType(sSuppliers.getTaxipayerStatus());
        synSupplierDataVo.setScDesc(sSuppliers.getRemarks());
        synSupplierDataVo.setSc_Address(sSuppliers.getHeadquartersAddress());
        synSupplierDataVo.setSc_Website(sSuppliers.getOfficialWebUrl());
        synSupplierDataVo.setSc_Remarks(sSuppliers.getRemarks());
        synSupplierDataVo.setSc_OfficeArea(StringUtils.object2StringNotNull(sSuppliers.getOfficeArea()));
        synSupplierDataVo.setSc_TotalEmpl(StringUtils.object2StringNotNull(sSuppliers.getStaffNumbers()));
        synSupplierDataVo.setSc_IsPublic(StringUtils.object2StringNotNull(sSuppliers.getCompanyListed()));
        return synDataService.synSynSupplierDataToOldSystem(synSupplierDataVo, isNew);
    }*/


    /**
     * 对接非五个三的供应商插口
     *
     * @param
     * @param isNew
     * @return
     */
//    @Override
    /*public boolean abutmentSuplierData(SSupplierManual sSupplierManual, boolean isNew) {

        SynSupplierDataVo synSupplierDataVo = new SynSupplierDataVo();

        if (sSupplierManual != null) {
            synSupplierDataVo.setId(sSupplierManual.getId());
            synSupplierDataVo.setSc_Name(sSupplierManual.getScName());
            synSupplierDataVo.setSc_Code(sSupplierManual.getScCode());
            synSupplierDataVo.setSc_Area(sysAreaService.getSysAreaCodesByIds(sSupplierManual.getScArea()));
            synSupplierDataVo.setSc_Level(sSupplierManual.getScLevel());
//			synSupplierDataVo.setSc_ShortName(sc_ShortName);
//			synSupplierDataVo.setSC_GradeTZ(SC_GradeTZ);
//			synSupplierDataVo.setSc_TypeName(sc_TypeName);
//			synSupplierDataVo.setSc_Type(sc_Type);
//			synSupplierDataVo.setSc_PdTypeName(sc_PdTypeName);
//			synSupplierDataVo.setSc_PdType(sc_PdType);
            synSupplierDataVo.setSc_LicenseNo(sSupplierManual.getLicenseNo());

            if (sSupplierManual.getStartupDate() != null) {
                synSupplierDataVo.setSc_StartupDate(DateUtils.formatDate(sSupplierManual.getStartupDate(), DateUtils.DATE_TIME));
            }

            if (sSupplierManual.getCreateDate() != null) {
                synSupplierDataVo.setSc_CheckinDate(DateUtils.formatDate(sSupplierManual.getCreateDate(), DateUtils.DATE_TIME));
            }
            synSupplierDataVo.setSc_RecommendPerson(sSupplierManual.getRecommendPerson()); //推荐类型

            //企业类别
            if (EnumConstant.enterpriseCharacterize.enterpriseCharacterize1.equals(sSupplierManual.getEquityType())) {
                synSupplierDataVo.setSc_EquityType(1); //内资
            } else if (EnumConstant.enterpriseCharacterize.enterpriseCharacterize2.equals(sSupplierManual.getEquityType())) {
                synSupplierDataVo.setSc_EquityType(3); //外资
            } else if (EnumConstant.enterpriseCharacterize.enterpriseCharacterize3.equals(sSupplierManual.getEquityType())) {
                synSupplierDataVo.setSc_EquityType(2); //合资
            } else if (EnumConstant.enterpriseCharacterize.enterpriseCharacterize4.equals(sSupplierManual.getEquityType())) {
                synSupplierDataVo.setSc_EquityType(4); //民营企业
            }

//			synSupplierDataVo.setSc_OprStatus(sc_OprStatus);
//			synSupplierDataVo.setSc_IsProduction(sc_IsProduction);
//			synSupplierDataVo.setSc_IsSignRequire(sc_IsSignRequire);
//			synSupplierDataVo.setSc_IsStrategic(sc_IsStrategic);
//			synSupplierDataVo.setSc_Agent(sc_Agent);
//			synSupplierDataVo.setSc_PurPartner(sc_PurPartner);
//			synSupplierDataVo.setSc_PmId(sc_PmId);
//			synSupplierDataVo.setSc_PmName(sc_PmName);
//			synSupplierDataVo.setSc_Affiliates(sc_Affiliates);
//			synSupplierDataVo.setSc_BiddingType(sc_BiddingType);
//			synSupplierDataVo.setSc_CooperationType(sc_CooperationType);
//			synSupplierDataVo.setSc_ServiceRegion(sc_ServiceRegion);
            synSupplierDataVo.setSc_taxpayerType(sSupplierManual.getTaxpayerType());
//			synSupplierDataVo.setScServiceRegionArea(scServiceRegionArea);
            synSupplierDataVo.setScDesc(sSupplierManual.getRemarks());
            synSupplierDataVo.setSc_Address(sSupplierManual.getScAddress());
            synSupplierDataVo.setSc_Website(sSupplierManual.getWebSite());
            synSupplierDataVo.setSc_Remarks(sSupplierManual.getRemarks());
            synSupplierDataVo.setSc_OfficeArea(sSupplierManual.getOfficeArea());
//			synSupplierDataVo.setSc_MatchResources(sc_MatchResources);
//			synSupplierDataVo.setSc_GovRelations(sc_GovRelations);
            synSupplierDataVo.setSc_TotalEmpl(sSupplierManual.getTotalEmpl());
            synSupplierDataVo.setSc_IsPublic(sSupplierManual.getPublicFlag());
//			synSupplierDataVo.setSc_StockCode(sc_StockCode);

            return synDataService.synSynSupplierDataToOldSystem(synSupplierDataVo, isNew);
        }

        return false;
    }*/
    @Override
    public boolean abutmentSuplierData2MDM(SSuppliers sSuppliers, boolean isNew) {
        SynSupplierToMdmDataVo synSupplierToMdmDataVo = new SynSupplierToMdmDataVo();

        synSupplierToMdmDataVo.setCODE(sSuppliers.getCompanyCode());
        //供方ID
        synSupplierToMdmDataVo.setSupplierId(sSuppliers.getId());
        // 供方名称
        synSupplierToMdmDataVo.setDESC1(sSuppliers.getCompanyName());
        //是否客户
//        synSupplierToMdmDataVo.setDESC4("否");
        //是否供方
        synSupplierToMdmDataVo.setDESC5("是");
        // 企业性质：集体企业、外商投资企业、港、澳、台商投资企业、联营企业、股份合作企业、股份有限公司、有限责任公司、其他内资企业、私营企业、国有企业
        synSupplierToMdmDataVo.setDESC7(EnumConstant.enterpriseCharacterize.getDesc(String.valueOf(sSuppliers.getEnterpriseCharacterize())));
        // 内部外部
        synSupplierToMdmDataVo.setDESC9("外部");
        // 统一社会信用代码
        synSupplierToMdmDataVo.setDESC10(sSuppliers.getSocialCreditCode());

        //状态（正常、停用。停用为公司注销）
        synSupplierToMdmDataVo.setDESC18("Y");
        // 注册资本 （人民币）
        synSupplierToMdmDataVo.setDESC19(String.valueOf(sSuppliers.getRegisteredCapital()));
        // 默认设置为 CNY
        synSupplierToMdmDataVo.setDESC20("CNY");
        // 企业成立日期
        synSupplierToMdmDataVo.setDESC21(DateUtils.formatDate(sSuppliers.getRegisterTime(), null));
        // 企业注册地址
        synSupplierToMdmDataVo.setDESC25(sSuppliers.getEnterpriseRegistAddress());
        // 营业期限 （截止日期），截止日期为空，视为长期
        if (sSuppliers.getBusinessYearLimitedEnd() != null) {
            synSupplierToMdmDataVo.setDESC27(DateUtils.formatDate(sSuppliers.getBusinessYearLimitedEnd(), null));
            synSupplierToMdmDataVo.setDESC26("N");
        } else {
            //是否长期
            synSupplierToMdmDataVo.setDESC26("Y");
        }
        // 法定代表人
        synSupplierToMdmDataVo.setDESC28(sSuppliers.getLegalRepresentative());
        if (StringUtils.isNotEmpty(sSuppliers.getLegalRepresentativeIdNumber())) {
            // 法定代表人证件类型（默认身份证)
            synSupplierToMdmDataVo.setDESC13("身份证");
            // 法定代表人身份证号
            synSupplierToMdmDataVo.setDESC14(sSuppliers.getLegalRepresentativeIdNumber());
        }
        // 纳税人类型（枚举值：0/1/2   0:一般纳税人/1:小规模纳税人/2:政府机关）
        synSupplierToMdmDataVo.setDESC15(EnumConstant.taxipayerStatus.getDesc(String.valueOf(sSuppliers.getTaxipayerStatus())));
        // 供方办公地址
        synSupplierToMdmDataVo.setDESC35(sSuppliers.getHeadquartersAddress());
        // 官网地址
        synSupplierToMdmDataVo.setDESC36(sSuppliers.getOfficialWebUrl());
        // 来源系统
//        synSupplierToMdmDataVo.setDESC42("腾云筑科采购管理系统");
        // 更新系统
//        synSupplierToMdmDataVo.setDESC43("腾云筑科采购管理系统");


        Multicode multicode = new Multicode();
        multicode.setVALUELIST(new ArrayList<>());
        synSupplierToMdmDataVo.setMULTICODE(multicode);
        //多值--联系人：
        SSupplierContactInfo sSupplierContactInfo = new SSupplierContactInfo();
        sSupplierContactInfo.setSuppliersId(sSuppliers.getId());
        sSupplierContactInfo.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.setEntity(sSupplierContactInfo);
        List<SSupplierContactInfo> sSupplierContactInfols = sSupplierContactInfoService.selectList(wrapper);
        int listcode = 10000;
        if (sSupplierContactInfols != null && sSupplierContactInfols.size() > 0) {
            Map<String, Object> existMap = new HashMap<>();
            for (SSupplierContactInfo contact : sSupplierContactInfols) {
                Map<String, String> map = new HashMap<>();
                // 节点类型，A08  指联系人
                StringBuilder sb = new StringBuilder(contact.getContactName()).append(contact.getPositionName()).append(contact.getMobile()).append(contact.getEmail());
                if (existMap.get(sb.toString()) != null) {
                    continue;
                }
                existMap.put(sb.toString(), contact);
                map.put("SPECIALITYCODE", "A07");
                //联系人姓名
                map.put("DESC1", contact.getContactName());
                //职位
                map.put("DESC2", contact.getPositionName());
                //手机号码
                map.put("DESC3", contact.getMobile());
                //电子邮箱
                map.put("DESC4", contact.getEmail());
                //节点编码
                map.put("LISTCODE", String.valueOf(++listcode));
                multicode.getVALUELIST().add(map);
            }
        }
        //系统来源,A11
        Map<String, String> LYXT = new HashMap<>();
        LYXT.put("SPECIALITYCODE", "A11");
        LYXT.put("LISTCODE", String.valueOf(++listcode));
        LYXT.put("DESC1", "TYZK_CGXT");
        multicode.getVALUELIST().add(LYXT);

        return synDataService.synSynSupplierData2MDM(synSupplierToMdmDataVo, isNew);
    }
}
